package com.dht.xiao.jianye.command;

import com.dht.xiao.jianye.base.serialport.utils.ByteUtil;
import com.licheedev.myutils.LogPlus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 命令结果处理
 */
public class CmdResult {
    /**
     * 开关投递的结果处理
     * */
    public static String Open_Close(String result){
        String hint = "";
        switch (result){
            case "00":hint = "开门成功";break;
            case "01":hint = "硬件故障，堵转";break;
            case "02":hint = "硬件故障，开门超时";break;
            case "03":hint = "触发防夹功能";break;
            case "FE":hint = "硬件故障，CRC校验失败";break;
            case "FF":hint = "硬件故障，超时未响应";break;
            default:hint = "硬件故障，异常回复";break;
        }
        return hint;
    }
    /**
    * 打开下仓门的结果处理
    * */
    public static String Open_next_door(String result){
        String hint = "";
        switch (result){
            case "00":hint = "开门成功";break;
            case "01":hint = "硬件故障，过载";break;
            case "02":hint = "硬件故障，开门超时";break;

            case "FE":hint = "硬件故障，CRC校验失败";break;
            case "FF":hint = "硬件故障，超时未响应";break;
            default:hint = "硬件故障，异常回复";break;
        }
        return hint;
    }

    /**
     * 去皮的结果处理
     * @param result
     * @return
     */
    public static String NO_weight(String result){
        String hint = "";
        switch (result){
            case "00":hint = "去皮成功";break;
            case "01":hint = "硬件故障，失败";break;
            case "FE":hint = "硬件故障，CRC校验失败";break;
            case "FF":hint = "硬件故障，超时未响应";break;
            default:hint = "硬件故障，异常回复";break;
        }
        return hint;
    }

    /**
     * 称重异常结果的处理
     * @param result
     * @return
     */
    public static String getweight(String result){
        String hint = "";
        switch (result){
            case "01":hint = "硬件故障，电子秤称重失败";break;
            case "02":hint = "硬件故障，电子秤数据未稳定";break;
            case "FE":hint = "硬件故障，CRC校验失败";break;
            case "FF":hint = "硬件故障，电子秤通讯故障";break;
            default:hint = "硬件故障，异常回复";break;
        }
        return hint;
    }

    /**
     * 称重异常结果的处理
     * @param result
     * @return
     */
    public static String getweight1(String result){
        String hint = "";
        switch (result){
            case "01":hint = "硬件故障，电子秤称重失败";break;
            case "02":hint = "硬件故障，电子秤数据未稳定";break;
            case "FE":hint = "硬件故障，CRC校验失败";break;
            case "FF":hint = "硬件故障，电子秤通讯故障";break;
            default:hint = "硬件故障，异常回复";break;
        }
        return hint;
    }

    /**
     * 对获取到的重量数据进行计算，这个有正负数
     * @param isReal  是否是真实显示获取到的数据
     * @param data
     * @return
     */
    public static double calculate_weight(boolean isReal,String data){
        double weight = 0.0;
        int a,b;//重量 = (xx*256+xx)*10g
        short weight_minus = ByteUtil.hexStr2decimal_minus(data);
//        LogPlus.e("weight_minus=" + weight_minus);
        if (weight_minus >= 0) {
            a = Integer.parseInt(data.substring(0, 2), 16);
            b = Integer.parseInt(data.substring(2, 4), 16);
            weight = ByteUtil.formatDouble2((double)((a * 256 + b) * 10) / 1000);
//            LogPlus.e("weight=" + weight+"kg");
            return weight;
        }else {
            String aa = ByteUtil.integer2HexStr(Math.abs(weight_minus));//负数取绝对值，之后转16进制
            aa = ByteUtil.covering_position(aa,4);
            a = Integer.parseInt(aa.substring(0, 2), 16);
            b = Integer.parseInt(aa.substring(2, 4), 16);
            weight = ByteUtil.formatDouble2((double)((a * 256 + b) * 10) / 1000);
            LogPlus.e("负数的重量weight=" + weight+"kg");
            if (!isReal) {
                if (weight > 400) {
                    weight = -weight;
                } else {
                    weight = 0.0;
                }
            }else {
                weight = -weight;
            }

        }
        return weight;
    }

    public static String getQuery( byte[] kk ){
        StringBuilder chickLock = new StringBuilder();
        Boolean isProblem = false;
        if (kk.length > 15){
            if (kk[0] == 0x01){
                chickLock.append("上仓门故障 "); isProblem = true;
            }
            if (kk[1] == 0x01){
                chickLock.append("下仓门故障 "); isProblem = true;
            }
            if (kk[2] == 0x01){
                chickLock.append("烟雾传感器故障 "); isProblem = true;
            }
            if (kk[3] == 0x01){
                chickLock.append("温度传感器故障 "); isProblem = true;
            }
            if (kk[4] == 0x01){
                chickLock.append("电子秤故障 "); isProblem = true;
            }
            if (kk[5] == 0x01){
                chickLock.append("激光传感器断开 "); isProblem = true;
            }
            if (kk[6] == 0x01){
                chickLock.append("电子秤故障 "); isProblem = true;
            }
            if (kk[7] == 0x01){
                chickLock.append("风扇异常 "); isProblem = true;
            }
            if (kk[8] == 0x01){
                chickLock.append("电表异常 "); isProblem = true;
            }
            if (kk[9] == 0x01){
                chickLock.append("外设异常"); isProblem = true;
            }
        }
        if (!isProblem){
            chickLock.append("无故障");
        }
        return chickLock.toString();
    }

    /**
     *
     * @param data   要处理的数据
     * @param sendlength 要按几个字节组成一份
     * @return
     */
    public static List<byte[]> getsplit_data(byte[] data,int sendlength){
        List<byte[]> result = new ArrayList<>();
        result = splitBytes(result128(data,sendlength),sendlength);
        return result;
    }

    /**
     * 固件数据，每条128字节，最后一帧不足128字节，补FF
     * @param result
     */
    private static byte[] result128(byte[] result,int sendlength){
        int result_short =0;
        int short_len =  result.length%sendlength;
        if (short_len != 0){
            result_short = sendlength - short_len;
            byte[] result_data = new byte[result.length+result_short];
            System.arraycopy(result, 0, result_data, 0, result.length);
            for (int i = 0; i < result_short;i++){
                result_data[result.length+i] = (byte) 0xFF;
            }
            return result_data;
        }

        return result;
    }

    /**
     * 拆分byte数组
     *
     * @param bytes
     *            要拆分的数组
     * @param size
     *            要按几个组成一份
     * @return
     */
    private static List<byte[]> splitBytes(byte[] bytes, int size) {
        double splitLength = Double.parseDouble(size + "");
        int arrayLength = (int) Math.ceil(bytes.length / splitLength);
        //byte[][] result = new byte[arrayLength][];
        List<byte[]> result = new ArrayList<>();
        int from, to;
        for (int i = 0; i < arrayLength; i++) {

            from = (int) (i * splitLength);
            to = (int) (from + splitLength);
            if (to > bytes.length)
                to = bytes.length;
            result.add(Arrays.copyOfRange(bytes, from, to));
        }
        return result;
    }
}
